﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using CoreGraphics;
using Foundation;
using Masonry;
using PagerManager;
using UIKit;

namespace mb.iOS
{
    public class MyCollectionViewCell : UICollectionViewCell
    {
        public object data;

        public MyCollectionViewCell(IntPtr handle) : base(handle)
        {
            data = null;
        }
    }

    public class MyCollectionDelegate : UICollectionViewDelegateFlowLayout
    {
        public override nfloat GetMinimumInteritemSpacingForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
        {
            return 0;
        }

        public override nfloat GetMinimumLineSpacingForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
        {
            var view = collectionView as MyCollectionView;
            return view.MyGetMinimumLineSpacingForSection(view, layout, (int) section);

        }
        public override UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
        {
            var view = collectionView as MyCollectionView;
            return view.MyGetInsetForSection(view, layout, (int) section);
        }

        public override CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
        {
            var view = collectionView as MyCollectionView;
            return view.MyGetSizeForItem(view, layout, indexPath);

        }

        public override void Scrolled(UIScrollView scrollView)
        {
            var view = scrollView as MyCollectionView;
            view.MyScrolled?.Invoke(view);
        }
        public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
        {
            var view = collectionView as MyCollectionView;
            view.MyItemSelected?.Invoke(view, indexPath);
        }
    }

    public class MyCollectionView : UICollectionView
    {
        public Func<MyCollectionView, int, int> MyGetItemsCount;
        public Func<MyCollectionView, UICollectionViewLayout, int, int> MyGetMinimumLineSpacingForSection;
        public Func<MyCollectionView, UICollectionViewLayout, int, UIEdgeInsets> MyGetInsetForSection;
        public Func<MyCollectionView, UICollectionViewLayout, NSIndexPath, CGSize> MyGetSizeForItem;
        public Func<MyCollectionView, NSIndexPath, MyCollectionViewCell> MyGetCell;
        public Func<MyCollectionView, NSIndexPath, UICollectionViewLayoutAttributes, UICollectionViewLayoutAttributes> MyGetLayoutAttributesForItem;
        public Action<MyCollectionView, NSIndexPath> MyItemSelected;
        public Action<MyCollectionView> MyScrolled;

        protected bool CellRegistered = false;
        public int SectionCount;

        public class MyCollectionViewSource : UICollectionViewSource
        {
            MyCollectionView CollectionView;
            public MyCollectionViewSource(MyCollectionView collectionView)
            {
                CollectionView = collectionView;
            }
            public override nint NumberOfSections(UICollectionView collectionView) => CollectionView.SectionCount;
            public override nint GetItemsCount(UICollectionView collectionView, nint section) => CollectionView.MyGetItemsCount(CollectionView, (int) section);
            public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) => CollectionView.MyGetCell(CollectionView, indexPath);

        }
        public override UICollectionViewLayoutAttributes GetLayoutAttributesForItem(NSIndexPath indexPath)
        {
            var attr = base.GetLayoutAttributesForItem(indexPath);
            if (MyGetLayoutAttributesForItem != null)
                return MyGetLayoutAttributesForItem(this, indexPath, attr);
            else
                return attr;
        }

        public MyCollectionView(int nsec) : base(CGRect.Empty, new UICollectionViewFlowLayout())
        {
            SectionCount = nsec;
            DataSource = new MyCollectionViewSource(this);
            Delegate = new MyCollectionDelegate();

            for (var i = 0; i < SectionCount; i++)
            {
                RegisterClassForCell(typeof(MyCollectionViewCell), "Cell" + i);
            }
        }

    }
}